package com.java.controller;

import com.java.Service.ReceiveFileFromBefore;
import com.java.result.Result;
import com.java.result.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: ZhangJin
 * @Date: 2020/9/20 22:16
 */
@RestController
@EnableAsync
public class UpLoadFileController {

    @Autowired
    ReceiveFileFromBefore receiveFileFromBefore;

    public static final String ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";


    // TODO: 2020/9/22   二手房文件上传
    @RequestMapping(value = "/api/multiImport", method = RequestMethod.POST)
    @ResponseBody
    public Result multiImport(MultipartFile[] uploadFile) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (MultipartFile multipartFile:uploadFile) {
            String[] split = Objects.requireNonNull(multipartFile.getOriginalFilename()).split("\\.");
            String Suffix = split[split.length - 1];
            String fileName = split[0];
            String contentType = multipartFile.getContentType();
            if (!contentType.equals(ContentType) && !Suffix.equals("xlsx"))
                return  ResultFactory.buildFailResult("上传的文件中存在格式问题,重新上传...");
            // TODO: 2020/9/20   开启异步线程来分析文件
            receiveFileFromBefore.AsyncCopeFile(multipartFile,fileName);
        }
        // TODO: 2020/9/20   判断是否是excel文件
        return ResultFactory.buildSuccessResult("success");
    }

    // TODO: 2020/9/22 附近商铺的选择
    @RequestMapping(value = "/api/nearByStoreInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result multiImport1(MultipartFile[] uploadFile) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (MultipartFile multipartFile:uploadFile) {
            String[] split = Objects.requireNonNull(multipartFile.getOriginalFilename()).split("\\.");
            String Suffix = split[split.length - 1];
            String fileName = split[0];
            String contentType = multipartFile.getContentType();
            if (!contentType.equals(ContentType) && !Suffix.equals("xlsx"))
                return  ResultFactory.buildFailResult("上传的文件中存在格式问题,重新上传...");
            // TODO: 2020/9/20   开启异步线程来分析文件
            receiveFileFromBefore.AsyncCopeFileNearByInfo(multipartFile,fileName);
        }
        // TODO: 2020/9/20   判断是否是excel文件
        return ResultFactory.buildSuccessResult("success");
    }
}
